#include <iostream>
#include <algorithm>

using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    for (int i = 2; i * i <= n; ++i) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    int n;
    cin >> n;

    if (n < 1000 || n > 9999) {
        cout << 0 << endl;
        return 0;
    }

    string s = to_string(n);
    sort(s.begin(), s.end());

    int ans = 0;
    do {
        int num = stoi(s);
        if (isPrime(num) && num > ans) {
            ans = num;
        }
    } while (next_permutation(s.begin(), s.end()));

    cout << ans << endl;

    return 0;
}
